package edu.study.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.PageRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.common.util.cache.Lock;
import edu.study.common.util.cache.LockExecuteCallback;
import edu.study.user.api.RoleApi;
import edu.study.user.api.dto.RoleDto;
import edu.study.user.api.req.role.AddRoleReq;
import edu.study.user.api.req.role.DeleteRoleReq;
import edu.study.user.api.req.role.ModifyRoleReq;
import edu.study.user.api.req.role.ModifyRoleStatusReq;
import edu.study.user.api.req.role.QueryRolePageListReq;
import edu.study.user.api.req.role.QueryRoleReq;
import edu.study.user.service.RoleService;

@RestController
public class RoleApiImpl implements RoleApi {

	@Autowired
    private Lock lock;

    @Autowired
    private RoleService roleService;

	@Override
	public BaseRespDto addRole(@RequestBody AddRoleReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getRoleName(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = roleService.addRole(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto deleteRole(@RequestBody DeleteRoleReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getRoleNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = roleService.deleteRole(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto modifyRole(@RequestBody ModifyRoleReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getRoleNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = roleService.modifyRole(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto modifyRoleStatus(@RequestBody ModifyRoleStatusReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getRoleNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = roleService.modifyRoleStatus(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public RespDto<RoleDto> queryRole(@RequestBody QueryRoleReq req) {
		RespDto<RoleDto> resp = roleService.queryRole(req);
        return resp;
	}

	@Override
	public PageRespDto<RoleDto> queryRolePageList(@RequestBody QueryRolePageListReq req) {
		PageRespDto<RoleDto> resp = roleService.queryRolePageList(req);
        return resp;
	}
}
